package xyz.hubery.event;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MimeTypeUtils;
import xyz.hubery.dto.TradeAreaAndMarketDTO;
import xyz.hubery.dto.TradeAreaDTO;
import xyz.hubery.dto.TradeMarketDTO;
import xyz.hubery.feign.MarketServiceFeign;
import xyz.hubery.feign.TradeAreaServiceFeign;
import xyz.hubery.model.MessagePayload;
import xyz.hubery.stream.Source;
import java.util.List;

/**
 * 行情市场的 K 线
 */
@Component
@Slf4j
public class MarketEvent implements Event {

    @Autowired
    private TradeAreaServiceFeign tradeAreaServiceFeign;

    @Autowired
    private Source source;

    /**
     * 交易区域频道
     */
    private static final String MARKET_CHANNEL = "market.%s.ticker";

    /**
     * 交易区域下的市场详情频道
     */
    private static final String MARKET_DETAIL_CHANNEL = "markets.%s.detail";

    @Override
    public void handle() {
        // 获取所有的交易区域及其管辖的交易市场
        List<TradeAreaAndMarketDTO> tradeAreaAndMarketDTOS = tradeAreaServiceFeign.listAreaAndMarkets();

        for (TradeAreaAndMarketDTO dto : tradeAreaAndMarketDTOS) {
            TradeAreaDTO areaDTO = dto.getTradeAreaDTO();
            List<TradeMarketDTO> tradeMarketDTOS = dto.getTradeMarketDTOS();

            if (CollectionUtils.isEmpty(tradeMarketDTOS)) {
                continue;
            }

            // 构建消息
            MessagePayload payload = new MessagePayload();
            payload.setChannel(String.format(MARKET_CHANNEL, areaDTO.getCode()));
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("markets", tradeMarketDTOS);
            payload.setBody(jsonObject.toJSONString());

            String s = jsonObject.toString();
            log.warn("MARKET_CHANNEL 消息内容大小：{}",s.getBytes().length);

            Message<MessagePayload> message = MessageBuilder.withPayload(payload).setHeader(MessageHeaders.CONTENT_TYPE,
                    MimeTypeUtils.APPLICATION_JSON).build();
            source.subscribeEventOutput().send(message);

            for (TradeMarketDTO tradeMarketDTO : tradeMarketDTOS) {
                // 构建消息
                MessagePayload payload1 = new MessagePayload();
                payload.setChannel(String.format(MARKET_DETAIL_CHANNEL, tradeMarketDTO.getSymbol().toLowerCase()));
                JSONObject jsonObject1 = new JSONObject();
                jsonObject.put("tick", tradeMarketDTO);
                payload.setBody(jsonObject1.toJSONString());


                String ss = jsonObject.toString();
                log.warn("MARKET_DETAIL_CHANNEL 消息内容大小：{}",ss.getBytes().length);


                Message<MessagePayload> message1 = MessageBuilder.withPayload(payload1).setHeader(MessageHeaders.CONTENT_TYPE,
                        MimeTypeUtils.APPLICATION_JSON).build();
                source.subscribeEventOutput().send(message1);
            }

        }

    }
}